package com.demo.fullink.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    
    //直接交换器队列
    public static final String ORDER_NOTIFY_QUEUE = "ORDER_NOTIFY_QUEUE";
    //直接交换器
    public static final String ORDER_NOTIFY_QUEUE_EXCHANGE = "ORDER_NOTIFY_QUEUE_EXCHANGE";
    //直接交换器ROUTINGKEY
    public static final String ORDER_NOTIFY_KEY = "ORDER_NOTIFY_KEY";
    
    //直接交换器队列
    public static final String PLACE_ORDER_QUEUE = "PLACE_ORDER_QUEUE";
    //直接交换器
    public static final String PLACE_ORDER_QUEUE_EXCHANGE = "PLACE_ORDER_QUEUE_EXCHANGE";
    //直接交换器ROUTINGKEY
    public static final String PLACE_ORDER_KEY = "PLACE_ORDER_KEY";
    
    @Bean
    public Queue orderNotifyQueue() {
        return new Queue(ORDER_NOTIFY_QUEUE);
    }
    
    //创建直接交换器
    @Bean
    DirectExchange orderNotifyQueueExchange() {
        return new DirectExchange(ORDER_NOTIFY_QUEUE_EXCHANGE);
    }
    
    @Bean
    public Queue placeOrderQueue() {
        return new Queue(PLACE_ORDER_QUEUE);
    }
    
    //创建直接交换器
    @Bean
    DirectExchange placeOrderQueueExchange() {
        return new DirectExchange(PLACE_ORDER_QUEUE_EXCHANGE);
    }
    
    //直接交换器和直接队列绑定
    @Bean
    Binding bindingOrderNotifyDirect() {
        return BindingBuilder.bind(orderNotifyQueue()).
                to(orderNotifyQueueExchange()).
                with(ORDER_NOTIFY_KEY);
    }
    
    //直接交换器和直接队列绑定
    @Bean
    Binding bindingPlaceOrderDirect() {
        return BindingBuilder.bind(placeOrderQueue()).
                to(placeOrderQueueExchange()).
                with(PLACE_ORDER_KEY);
    }
    
    @Bean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME)
    public RabbitListenerEndpointRegistry defaultRabbitListenerEndpointRegistry() {
        return new RabbitListenerEndpointRegistry();
    }
    
}
